package com.yxhl.zoume.core.func.webpage.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.specialcar.activity.PassengerNoticeActivity;
import com.yxhl.zoume.core.func.webpage.activity.WebPageActivity;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;

import butterknife.BindView;

/**
 * Created by vhawk on 16/4/12.
 */
public class WebViewFragment extends BaseFragment {

    private static final String BUNDLE_EXTRA_PARAM = "bundle_extra_param";

    @BindView(R.id.webView)
    public WebView webView;

    private WebPageEntrance mWebPageEntrance;


    public static WebViewFragment newInstance(WebPageEntrance entrance) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_EXTRA_PARAM, entrance);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        handleArguments();
        View view = super.onCreateView(inflater, container, savedInstanceState);
        initWeb();
        return view;
    }

    private void handleArguments() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mWebPageEntrance = (WebPageEntrance) bundle.getSerializable(BUNDLE_EXTRA_PARAM);
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initWeb() {
        if (mWebPageEntrance != null) {
            String url = mWebPageEntrance.getUrl();
            if (!TextUtils.isEmpty(url)) {
                webView.getSettings().setAppCacheEnabled(true);
                webView.getSettings().setSupportZoom(true);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

                webView.loadUrl(url);
                webView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });
            }
        }
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_func_web;
    }

    @Override
    protected void setupToolBar() {
        switch (mWebPageEntrance) {
            case SPECIAL_CAR_PASSENGER_NOTICE:
                ((PassengerNoticeActivity) mActivity).setToolbarTitle(getString(R.string.passenger_must_know));
                break;
            case TICKET_CODE_HELP:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.ticket_code_help_title));
                break;
            case ZOU_ME_BUS_INTRODUCTION:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.zoume_bus_header_title));
                break;
            case FIXED_BUS_INTRODUCTION:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.fixed_bus_header_title));
                break;
            case TICKET_BUS_INTRODUCTION:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.ticket_bus_header_title));
                break;
            case SPECIAL_CAR_PRICE_INTRODUCTION:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.special_car_price_ruler));
                break;
            case ABOUT_US:
                ((WebPageActivity) mActivity).setToolbarTitle("关于走么");
                break;
            case VERSION_INTROCUCTION:
                ((WebPageActivity) mActivity).setToolbarTitle("版本介绍");
                break;
            case WALLET_INTRODUCTIONS:
                ((WebPageActivity) mActivity).setToolbarTitle("钱包常见问题");
                break;
            case HELP_BUS:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.help_bus));
                break;
            case HELP_EXPRESS:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.help_express));
                break;
            case HELP_SPECIAL:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.help_special));
                break;
            case HELP_OFFICIAL:
                ((WebPageActivity) mActivity).setToolbarTitle(getString(R.string.help_official));
                break;
            case USER_AGREEMENT:
                ((WebPageActivity) mActivity).setToolbarTitle("用户协议");
                break;
            case PRICE_RULES_DETAIL:
                ((WebPageActivity) mActivity).setToolbarTitle("详细说明");
                break;
            case TICKET_BUS_REFUND_NOTICE:
                ((WebPageActivity) mActivity).setToolbarTitle("乘客须知");
                break;
        }
    }
}
